package org.jeecg.modules.scripts.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.scripts.entity.BlogScripts;
import org.jeecg.modules.scripts.service.BlogScriptsService;
import org.jeecg.config.shiro.IgnoreAuth;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: blog_scripts
 * @Author: jeecg-boot
 * @Date: 2025-06-30
 * @Version: V1.0
 */
@Tag(name = "blog_scripts")
@RestController
@RequestMapping("/blog/scripts")
@Slf4j
public class BlogScriptsController extends JeecgController<BlogScripts, BlogScriptsService> {
    @Autowired
    private BlogScriptsService blogScriptsService;

    /**
     * 分页列表查询
     *
     * @param blogScripts
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "blog_scripts-分页列表查询")
    @Operation(summary = "blog_scripts-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<BlogScripts>> queryPageList(BlogScripts blogScripts,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<BlogScripts> queryWrapper = QueryGenerator.initQueryWrapper(blogScripts, req.getParameterMap());
        Page<BlogScripts> page = new Page<BlogScripts>(pageNo, pageSize);
        IPage<BlogScripts> pageList = blogScriptsService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 公开的脚本列表查询（无需权限）
     *
     * @param pageNo
     * @param pageSize
     * @param name
     * @return
     */
    @Operation(summary = "公开的脚本列表查询")
    @GetMapping(value = "/public/list")
    @IgnoreAuth
    public Result<IPage<BlogScripts>> getPublicScripts(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "name", required = false) String name) {
        QueryWrapper<BlogScripts> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "active") // 只查询已发布的脚本
                .orderByDesc("create_time");

        // 如果有名称搜索
        if (name != null && !name.trim().isEmpty()) {
            queryWrapper.like("name", name.trim());
        }

        Page<BlogScripts> page = new Page<BlogScripts>(pageNo, pageSize);
        IPage<BlogScripts> pageList = blogScriptsService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 公开的脚本详情查询（无需权限）
     *
     * @param id
     * @return
     */
    @Operation(summary = "公开的脚本详情查询")
    @GetMapping(value = "/public/detail")
    @IgnoreAuth
    public Result<BlogScripts> getPublicScriptDetail(@RequestParam(name = "id", required = true) String id) {
        QueryWrapper<BlogScripts> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id)
                .eq("status", "active"); // 只查询已发布的脚本
        BlogScripts script = blogScriptsService.getOne(queryWrapper);
        if (script == null) {
            return Result.error("脚本不存在或未发布");
        }
        return Result.OK(script);
    }

    /**
     * 添加
     *
     * @param blogScripts
     * @return
     */
    @AutoLog(value = "blog_scripts-添加")
    @Operation(summary = "blog_scripts-添加")
    @RequiresPermissions("scripts:blog_scripts:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody BlogScripts blogScripts) {
        blogScriptsService.save(blogScripts);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param blogScripts
     * @return
     */
    @AutoLog(value = "blog_scripts-编辑")
    @Operation(summary = "blog_scripts-编辑")
    @RequiresPermissions("scripts:blog_scripts:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody BlogScripts blogScripts) {
        blogScriptsService.updateById(blogScripts);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "blog_scripts-通过id删除")
    @Operation(summary = "blog_scripts-通过id删除")
    @RequiresPermissions("scripts:blog_scripts:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        blogScriptsService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "blog_scripts-批量删除")
    @Operation(summary = "blog_scripts-批量删除")
    @RequiresPermissions("scripts:blog_scripts:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.blogScriptsService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "blog_scripts-通过id查询")
    @Operation(summary = "blog_scripts-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<BlogScripts> queryById(@RequestParam(name = "id", required = true) String id) {
        BlogScripts blogScripts = blogScriptsService.getById(id);
        if (blogScripts == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(blogScripts);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param blogScripts
     */
    @RequiresPermissions("scripts:blog_scripts:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BlogScripts blogScripts) {
        return super.exportXls(request, blogScripts, BlogScripts.class, "blog_scripts");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("scripts:blog_scripts:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BlogScripts.class);
    }

}
